package com.ym.b2b.order.dao.impl;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.dao.DataAccessException;
import org.springframework.orm.ibatis.SqlMapClientCallback;
import org.springframework.stereotype.Repository;

import com.ibatis.sqlmap.client.SqlMapExecutor;
import com.ym.b2b.commons.dao.BaseDao;
import com.ym.b2b.order.dao.EmSOrderConfirmItemDao;
import com.ym.b2b.order.sqlbean.EmSOrderConfirmItem;
import com.zc.commons.base.Page;

@Repository
public class EmSOrderConfirmItemDaoImpl<T> extends BaseDao<T> implements EmSOrderConfirmItemDao {

    @Override
    public int saveOrUpdateEmSOrderConfrimItem(EmSOrderConfirmItem emSOrderConfirmItem) throws DataAccessException {
        if (emSOrderConfirmItem.getOrderConfirmItemId() != null
            && !"".equals(emSOrderConfirmItem.getOrderConfirmItemId())) {

            // update
            update("EmSOrderItem.updateEmSOrderConfirmItem", emSOrderConfirmItem);
        } else {
            // save
            insert("EmSOrderItem.insertEmSOrderConfirmItem", emSOrderConfirmItem);
        }
        return 0;
    }

    @Override
    public void saveEmSOrderConfirmItemBatch(final List<EmSOrderConfirmItem> list) throws DataAccessException {
        getSqlMapClientTemplate().execute(new SqlMapClientCallback<Object>() {

            @Override
            public Object doInSqlMapClient(SqlMapExecutor executor) throws SQLException {
                executor.startBatch();
                for (EmSOrderConfirmItem l : list) {
                    if (null != l) {
                        executor.insert("EmSOrderItem.insertEmSOrderConfirmItem", l);
                    }
                }
                return executor.executeBatch();
            }
        });
    }

    @Override
    public List<EmSOrderConfirmItem> searchEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem)
                                                                                                       throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSOrderConfirmItem);
        return list("EmSOrderItem.searchEmSOrderConfirmItem", map);
    }

    @Override
    public void updateEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem) throws DataAccessException {
        update("EmSOrderItem.updateEmSOrderConfirmItem", emSOrderConfirmItem);

    }

    @Override
    public void deleteEmSOrderConfirmItem(EmSOrderConfirmItem emSOrderConfirmItem) throws DataAccessException {
        delete("EmSOrderItem.deleteEmSOrderConfirmItem", emSOrderConfirmItem);

    }

    @Override
    public Integer searchEmSOrderConfirmItemNum(EmSOrderConfirmItem emSOrderConfirmItem) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSOrderConfirmItem);
        return (Integer) find("EmSOrderItem.searchEmSOrderConfirmItemNum", map);
    }

    @Override
    public EmSOrderConfirmItem searchEmSOrderConfirmItemDetail(EmSOrderConfirmItem emSOrderConfirmItem)
                                                                                                       throws DataAccessException {

        return (EmSOrderConfirmItem) find("EmSOrderItem.searchEmSOrderConfirmItemDetail", emSOrderConfirmItem);
    }

    @Override
    public Integer searchEmSOrderConfirmItemBatchNum(EmSOrderConfirmItem emSOrderConfirmItem)
                                                                                             throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emSOrderConfirmItem);
        return (Integer) find("EmSOrderItem.searchEmSOrderConfirmItemBatchNum", map);
    }

    @Override
    public Long searchPageEmSOrderConfirmItemNum(EmSOrderConfirmItem emsorderconfirmitem) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emsorderconfirmitem);

        return (Long) find("EmSOrderItem.searchPageEmSOrderConfirmItemNum", map);

    }

    @Override
    public void searchPageEmSOrderConfirmItem(EmSOrderConfirmItem emsorderconfirmitem, Page<EmSOrderConfirmItem> page)
                                                                                                                      throws DataAccessException {
        page.setTotalRow(searchPageEmSOrderConfirmItemNum(emsorderconfirmitem));
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("map", emsorderconfirmitem);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmSOrderItem.searchPageEmSOrderConfirmItem", map));

    }

    @Override
    public Page<EmSOrderConfirmItem> searchPageItemReturn(EmSOrderConfirmItem emsorderconfirmitem,
                                                          Page<EmSOrderConfirmItem> page) throws DataAccessException {
        page.setTotalRow(searchPageItemNum(emsorderconfirmitem));
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("map", emsorderconfirmitem);
        map.put("startrow", page.getStartRow());
        map.put("pagesize", page.getPageSize());
        map.put("pageable", page.getPageable());
        page.setRows(list("EmSOrderItem.searchPageOrderConfirmItemReturn", map));
        return page;

    }

    @Override
    public Long searchPageItemNum(EmSOrderConfirmItem emsorderconfirmitem) throws DataAccessException {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("map", emsorderconfirmitem);

        return (Long) find("EmSOrderItem.searchPageOrderConfirmItemReturnNum", map);

    }

    @Override
    public void updateConfirmItemForIsReturn(EmSOrderConfirmItem emsorderconfirmitem) throws DataAccessException {
        update("EmSOrderItem.updateConfirmItemForIsReturn", emsorderconfirmitem);
    }

    @Override
    public void updateMultipleConfirmItemForIsReturns(EmSOrderConfirmItem emsorderconfirmitem) {
        update("EmSOrderItem.updateMultipleConfirmItemForIsReturns", emsorderconfirmitem);
    }
}
